<?php
namespace app\index\controller;

use \think\Db;

class Index extends BaseController
{
    public function index()
    {
        return '<style type="text/css">*{ padding: 0; margin: 0; } .think_default_text{ padding: 4px 48px;} a{color:#2E5CD5;cursor: pointer;text-decoration: none} a:hover{text-decoration:underline; } body{ background: #fff; font-family: "Century Gothic","Microsoft yahei"; color: #333;font-size:18px} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.6em; font-size: 42px }</style><div style="padding: 24px 48px;"> <h1>:)</h1><p> ThinkPHP V5<br/><span style="font-size:30px">十年磨一剑 - 为API开发设计的高性能框架</span></p><span style="font-size:22px;">[ V5.0 版本由 <a href="http://www.qiniu.com" target="qiniu">七牛云</a> 独家赞助发布 ]</span></div><script type="text/javascript" src="http://tajs.qq.com/stats?sId=9347272" charset="UTF-8"></script><script type="text/javascript" src="http://ad.topthink.com/Public/static/client.js"></script><thinkad id="ad_bd568ce7058a1091"></thinkad>';
    }

    public function test()
    {
        echo "test";
    }

    public function json()
    {
        $box = array('aa' => 123, 'bb' => 234);
        var_dump($box);
        var_dump($box["aa"]);
    }

    public function ajax11()
    {
        return $this->fetch('ajax');
    }

    public function saveUdetior()
    {
        if (Request::instance()->isOptions()) {
            echo json_encode([
                "code" => 200
            ]);
            exit;
        }

        try {
            // header("Content-Type: application/json");
            // echo $_POST;
            // var_dump($_POST);
            $res = \think\Db::table('tb_article')->insert($_POST);
            var_dump($res);
            exit;
        } catch (Exception $e) {
            var_dump($e->getMessage());
        }
    }

    public function showUdetior()
    {
        try {
            header("Content-Type: application/json");
            $res = \think\Db::table('tb_article')->where('id', $_GET["id"])->find();

            $res["content2"] = htmlspecialchars_decode($res["content"]);
            $res["content3"] = html_entity_decode($res["content2"]);


            echo json_encode([
                "code" => 200,
                "data" => $res
            ]);
            exit;
        } catch (Exception $e) {
            var_dump($e->getMessage());
        }

    }
    
    
    public function demo()
    {
        $name = 'name_cn';
        $duty = 'duty_cn';
        $content = 'content_cn';

        $meeting = \think\Db::query("select p.people_id,p.$name,y.duty_id,y.$duty,p.s_img_url,p.team_id,i.intro_id,i.$content from ls_people p join ls_people_intro i on p.people_id=i.people_id join ls_duty y on p.people_id=y.people_id where p.delete_time is null and p.team_id=3");

        // $result = \think\Db::query("SELECT p.people_id,p.name_cn,i.intro_id,i.content_cn,d.duty_id,d.duty_cn FROM ls_people p LEFT JOIN ls_people_intro i ON i.`people_id`=p.`people_id` JOIN ls_duty d ON d.`people_id`=p.`people_id` WHERE p.team_id=2");

        // return json($meeting);
        
        $content_list = 'content_cn_list';
        $duty_list = 'duty_cn_list';

        $con = [];
        $temp1 = [];// 存放 people_id
        $temp2 = [];// 存放 duty_id
        $temp3 = [];// 存放 intro_id

        foreach ($meeting as $k=>$v){
            if (!in_array($v['people_id'], $temp1)) {
                array_push($temp1, $v['people_id']);
                array_push($temp2, $v['duty_id']);
                array_push($temp3, $v['intro_id']);    
                
                $v[$content_list] = [$v[$content]];
                $v[$duty_list] = [$v[$duty]];

                unset($v[$content]);
                unset($v[$duty]);
                unset($v['duty_id']);
                unset($v['intro_id']);

                $con[] = $v;

            } else {
                foreach ($con as $a => $b) {
                    if($b["people_id"] == $v['people_id']){
                        if (!in_array($v['duty_id'], $temp2)) {
                            $con[$a][$duty_list][] = $v[$duty];
                            array_push($temp2, $v['duty_id']);    
                        }
                        if (!in_array($v['intro_id'], $temp3)) {
                            $con[$a][$content_list][] = $v[$content];
                            array_push($temp3, $v['intro_id']);    
                        }
                    }
                }
            }
        }
        return json($con);
    }
    
    // ueditor 两个同时存在时的bug
    public function ueditor()
    {
        $this->assign('stage', [
            'stage_title_cn' => '中文标题',
            'stage_title_en' => '英文标题',
            'news_time' => '时间',
            'content_cn' => '<h1>中文内容</h1>',
            'content_en' => '<strong>英文
            内容</strong>',
        ]);
        return $this->fetch('ueditor2');
    }

    public function Kv()
    {
        $res = \app\org\Kv::Kv();
        var_dump($res[0]['server_url']);exit;
    }

    public function cmd()
    {
        var_dump($_SERVER);exit;
    }
}
